package bbcom.messages;

import org.apache.commons.lang3.StringUtils;

import java.text.MessageFormat;
import java.util.ResourceBundle;

/**
 * Created by Thanh-Trung Pham.
 */
public class BBCoMMessageUtility {
    private static final String BBCOM_MESSAGE_SERCOMPMESSAGERESOURCE = "bbcom.messages.BBCoMMessageResource";

    private static ResourceBundle m_oResourceBundle;

    /**
     * Get the resource message by ID.
     *
     * @param resourceMessageID the resource message ID.
     * @return the resource message.
     */
    public static String getResourceMessage(String resourceMessageID) {
        if (m_oResourceBundle == null) {
            m_oResourceBundle = ResourceBundle.getBundle(BBCOM_MESSAGE_SERCOMPMESSAGERESOURCE);
        }

        return m_oResourceBundle.getString(resourceMessageID);
    }

    /**
     * Get the resource message by ID, replace the pattern {0} with the given parameter.
     *
     * @param resourceMessageID the resource message ID.
     * @param parameter         the parameter.
     * @return the resource message.
     */
    public static String getResourceMessage(String resourceMessageID, String parameter) {
        String resourceMessage = getResourceMessage(resourceMessageID);

        return MessageFormat.format(resourceMessage, new Object[]{parameter});
    }

    /**
     * Get the resource message by ID, replace the pattern {0} with the given parameter A, {1} with the given parameter B.
     *
     * @param resourceMessageID the resource message ID.
     * @param parameterA        the parameter A.
     * @param parameterB        the parameter B.
     * @return the resource message
     */
    public static String getResourceMessage(String resourceMessageID, String parameterA, String parameterB) {
        String resourceMessage = getResourceMessage(resourceMessageID);

        return MessageFormat.format(resourceMessage, new Object[]{parameterA, parameterB});
    }

    /**
     * Get the resource message by ID, replace the pattern {0} with the given parameter A, {1} with the given parameter B, {2} with the given parameter C.
     *
     * @param resourceMessageID the resource message ID.
     * @param parameterA        the parameter A.
     * @param parameterB        the parameter B.
     * @param parameterC        the parameter C.
     * @return the resource message
     */
    public static String getResourceMessage(String resourceMessageID, String parameterA, String parameterB, String parameterC) {
        String resourceMessage = getResourceMessage(resourceMessageID);

        return MessageFormat.format(resourceMessage, new Object[]{parameterA, parameterB, parameterC});
    }

    /**
     * Get the resource message by ID, replace the pattern {0} with the given exception message.
     *
     * @param resourceMessageID the resource message ID.
     * @param exception         the exception.
     * @return the resource message.
     */
    public static String getResourceMessage(String resourceMessageID, Exception exception) {
        String resourceMessage = getResourceMessage(resourceMessageID);

        String message = exception.getClass().getName() + ": " + exception.getMessage();

        return MessageFormat.format(resourceMessage, new Object[]{message});
    }

    /**
     * Get the resource message by ID, replace the pattern {0} with the given parameter, {1} with the given exception message.
     *
     * @param resourceMessageID the resource message ID.
     * @param parameter         the parameter.
     * @param exception         the exception.
     * @return the resource message.
     */
    public static String getResourceMessage(String resourceMessageID, String parameter, Exception exception) {
        String resourceMessage = getResourceMessage(resourceMessageID);

        String exceptionMessage = exception.getMessage();
        if (StringUtils.isBlank(exceptionMessage)) {
            exceptionMessage = exceptionMessage.getClass().getName();
        }

        return MessageFormat.format(resourceMessage, new Object[]{parameter, exceptionMessage});
    }

}
